<?php

namespace app\admin\model\workorder;

use think\Model;
use traits\model\SoftDelete;

class Engineers extends Model
{

    use SoftDelete;


    // 表名
    protected $name = 'workorder_engineers';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = false;
    protected $deleteTime = 'deletetime';

    // 追加属性
    protected $append = [
        'class_text',
        'status_text',
        'avg_response_time_text'
    ];


    public function getClassList()
    {
        return ['0' => __('Class 0'), '1' => __('Class 1')];
    }

    public function getStatusList()
    {
        return ['0' => __('Status 0'), '1' => __('Status 1')];
    }


    public function getClassTextAttr($value, $data)
    {
        $value = $value ?: ($data['class'] ?? '');
        $list  = $this->getClassList();
        return $list[$value] ?? '';
    }


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ?: ($data['status'] ?? '');
        $list  = $this->getStatusList();
        return $list[$value] ?? '';
    }

    public function getAvgResponseTimeTextAttr($value, $data)
    {
        $value = $value ?: ($data['avg_response_time'] ?? 0);
        $h     = floor($value / 3600);
        $m     = ceil(($value % 3600) / 60);
        return $value ? __('%s hour %s minute', [$h, $m]) : __('No data');
    }


    public function user()
    {
        return $this->belongsTo('app\admin\model\User', 'user_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
}
